package com.example.demo.util;

import org.apache.commons.io.FilenameUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;


public class FileUtils {

    public static String IMG_TYPE_PNG = "PNG";
    public static String IMG_TYPE_JPG = "JPG";
    public static String IMG_TYPE_JPEG = "JPEG";
    public static String V_TYPE_MP4 = "MP4";
    public static String E_TYPE_XLS = "XLS";
    public static String E_TYPE_XLSX = "XLSX";

    public static List<String>  getImageList(MultipartFile[] file) throws IOException {
        List<String> list = new ArrayList<String>();
        if (file != null) {
            String path = "";
            for (MultipartFile mf : file) {
                if (!mf.isEmpty()) {
                    String suffix = mf.getOriginalFilename().substring(mf.getOriginalFilename().lastIndexOf(".") + 1);
                    if(!(IMG_TYPE_PNG.equalsIgnoreCase(suffix.toUpperCase()) ||
                            (IMG_TYPE_JPG.equalsIgnoreCase(suffix.toUpperCase()))||
                            (IMG_TYPE_JPEG.equalsIgnoreCase(suffix.toUpperCase())))) {
                        throw new IOException();
                    }else{
                        String uuid = UUID.randomUUID().toString();
                        String originalFilename = mf.getOriginalFilename();
                        String ext= "." + FilenameUtils.getExtension(originalFilename);//获取拓展名字.jpg
                        String fileName =uuid + ext;//xxxxxsfsasa.jpg
                        path = "/images/" + fileName;
                        mf.transferTo(new File("D:/inetpub/wwwroot" + path));
                        list.add(path);
                    }
                }
            }
        }
        return  list;
    }

    public static List<String>  getVideoList(MultipartFile[] file) throws IOException {
        List<String> list = new ArrayList<String>();
        if (file != null) {
            String path = "";
            for (MultipartFile mf : file) {
                if (!mf.isEmpty()) {
                    String suffix = mf.getOriginalFilename().substring(mf.getOriginalFilename().lastIndexOf(".") + 1);
                    if(!V_TYPE_MP4.equalsIgnoreCase(suffix.toUpperCase())) {
                        throw new IOException();
                    }else{
                        String contentType = mf.getContentType();
                        String imageName = contentType.substring(contentType.indexOf("/") + 1);
                        path = "/videos/ "+"." + imageName;
                        mf.transferTo(new File("D:/inetpub/wwwroot" + path));
                    }
                }
            }
        }
        return  list;
    }

    public static String getOnImage(MultipartFile file) throws IOException {
        String path = "";
        if (file != null) {
            String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".") + 1);
            System.out.println();
            if(!(IMG_TYPE_PNG.equalsIgnoreCase(suffix.toUpperCase()) ||
                    IMG_TYPE_JPG.equalsIgnoreCase(suffix.toUpperCase()) ||
                    IMG_TYPE_JPEG.equalsIgnoreCase(suffix.toUpperCase()))) {
                throw new IOException();
            }else{
                String uuid = UUID.randomUUID().toString();
                String originalFilename = file.getOriginalFilename();
                String ext= "." + FilenameUtils.getExtension(originalFilename);//获取拓展名字.jpg
                String fileName =uuid + ext;//xxxxxsfsasa.jpg
                path = "/images/"+fileName;
                file.transferTo(new File("D:/inetpub/wwwroot" + path));
            }
        }
        return  path;
    }

    public static String getOnVideo(MultipartFile file) throws IOException {
        String path = "";
        if (file != null) {
            if (!file.isEmpty()) {

                String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".") + 1);
                if(!V_TYPE_MP4.equalsIgnoreCase(suffix.toUpperCase())) {
                    throw new IOException();
                }else{
                    //获得文件类型（可以判断如果不是图片，禁止上传）
                    String contentType = file.getContentType();
                    //获得文件后缀名称
                    String imageName = contentType.substring(contentType.indexOf("/") + 1);
                    path = "/videos/"+ "." + imageName;
                    file.transferTo(new File("D:/inetpub/wwwroot" + path));
                }
            }
        }
        return  path;
    }

    public static String uploadExcel(MultipartFile file) throws IOException {
        String path = "";
        if (file != null) {
            String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".") + 1);
            System.out.println();
            if(!(E_TYPE_XLS.equalsIgnoreCase(suffix.toUpperCase()) ||
                    E_TYPE_XLSX.equalsIgnoreCase(suffix.toUpperCase()))) {
                throw new IOException();
            }else{
                path = "/excel/"+file.getOriginalFilename();
                file.transferTo(new File("D:/OccupationInfoExcel/" + path));
            }
        }
        return  path;
    }

}
